﻿@using IEMSOFT.EasyHotel.Admin.Models;
@using IEMSOFT.EasyHotel.Common;
@using IEMSOFT.Foundation;
@{
    ViewBag.Title = "账号管理";
}
@section header
{
}
@{
    var currentUser = ViewBag.CurrentUser as UserModel;
}
<table id="dg" title="账号管理" class="easyui-datagrid"
    url="/account/get"
    toolbar="#toolbar" pagination="false"
    rownumbers="true" fitcolumns="true" singleselect="false" ctrlselect="true" striped="true">
    <thead>
        <tr>
            <th field="userName">系统账号名</th>
            <th field="password">系统密码</th>
            <th field="fullName">姓名</th>
            <th field="roleName">职务</th>
            <th field="subHotelName">所在分店</th>
        </tr>
    </thead>
</table>

<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newAdd()">新增</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="edit()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="destory()">删除</a>
</div>

<div id="dlg" class="easyui-dialog" style="width: 400px; height: 260px; padding: 10px 20px"
    closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post" data-options="novalidate:true">
        <input name="userId" type="hidden" />
        <div class="fitem">
            <label>系统账号名:</label>
            <input name="userName" class="easyui-textbox" required="true" id="txtUserName" validtype="length[5,15]" />
        </div>
        <div class="fitem">
            <label>系统密码:</label>
            <input name="password" class="easyui-textbox" required="true" validtype="length[6,15]" />
        </div>
        <div class="fitem">
            <label>姓名:</label>
            <input name="fullName" class="easyui-textbox" required="true" />
        </div>
        <div class="fitem">
            <label>职务:</label>
            <select id="cbRoleName" class="easyui-combobox" name="roleId"
                data-options="url:'/role/FilterOption',method:'get',valueField:'value',textField:'text'">
            </select>
        </div>
        @if (currentUser.RoleId == RoleType.GroupLeader.ToInt())
        {  
            <div class="fitem">
                <label>所在分店:</label>
                <select id="cbSubHotel" class="easyui-combobox" name="subHotelId"
                    data-options="url:'/subhotel/option',method:'get',valueField:'value',textField:'text'">
                </select>
            </div>
        }
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconcls="icon-ok" onclick="save()" style="width: 90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width: 90px">取消</a>
</div>
@section Scripts
{
<script type="text/javascript">
    var msgTitle = '账号管理';
    var actionUrl = '';
    var roleIdForCombobox = '@ViewBag.RoleIdForCombobox';
    function newAdd() {
        $('#dlg').dialog('open').dialog('setTitle', '新增账号');
        $('#fm').form('clear');
        $('#cbSubHotel').combobox('setValue', '0');
        $('#cbRoleName').combobox('setValue', roleIdForCombobox);
        actionUrl = "/account/add";
        $('#txtUserName').textbox('readonly', false);
    }
    function edit() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('setTitle', '编辑账号');
            $('#txtUserName').textbox('readonly', true);
            $('#fm').form('load', row);
            actionUrl = "/account/update";
            $('#fm input[name=password]').prev().focus();//设置焦点
        }
    }
    function save() {
        if (!$('#fm').form('enableValidation').form('validate')) {
            return;
        }
        var roleValue = $('#cbRoleName').combobox('getValue');
        var subHotelValue = '@currentUser.SubHotelId';
        if (subHotelValue == '0') {
            subHotelValue = $('#cbSubHotel').combobox('getValue');
        };
        if (roleType.groupLeader == roleValue && subHotelValue != '0') {
            $.messager.alert(msgTitle, '职务为集团领导时不需要选择分店！', 'error');
            return;
        } else if (roleValue != roleType.groupLeader && subHotelValue == '0') {
            $.messager.alert(msgTitle, '请选择分店！', 'error');
            return;
        }
        $.post(actionUrl,
                 $('#fm').serialize(),
                  function (data) {
                      var msg = '';
                      if (data.msg.length != 0) {
                          $.each(data.msg, function () {
                              msg = data.msg + msg + '<br/>';
                          });
                          $.messager.alert(msgTitle, msg, 'error');
                      }
                      else {
                          $('#dlg').dialog('close'); // close the dialog
                          $('#dg').datagrid('reload'); // reload data
                      }
                  });
    }
    function destory() {
        actionUrl = '/account/remove';
        var rows = $('#dg').datagrid('getSelections');
        if (rows.length > 0) {
            $.messager.confirm('Confirm', '确实要删除吗?', function (r) {
                if (r) {
                    $.post(actionUrl,
                          JSON.stringify(rows),
                          function (data) {
                              var msg = '';
                              if (data.msg.length != 0) {
                                  $.each(data.msg, function (index, item) {
                                      msg = msg + item + '<br/>';
                                  });
                                  $.messager.alert(msgTitle, msg, 'error');
                              }
                              else {
                                  $('#dlg').dialog('close'); // close the dialog
                                  $('#dg').datagrid('reload'); // reload data
                              }
                          });
                }
            });
        }
    }
</script>
}